package utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HttpClientUtils {
    //初始化httpClient
    private static final CloseableHttpClient httpClient;
    private static final Logger log= LoggerFactory.getLogger(HttpClientUtils.class);
    static {
        httpClient= HttpClientBuilder.create().build();
    }
    //构造url
    public static  String  build_url(String url, Map<String,String> query_path) throws URISyntaxException {
        URIBuilder uriBuilder=new URIBuilder(url);
        if(query_path!=null){
            for(String key: query_path.keySet()){
                String value= query_path.get(key);
                uriBuilder.addParameter(key,value);
            }
        }
        return uriBuilder.build().toString();
    }
    //构造响应数据
    public static ResponseData build_response(CloseableHttpResponse response) throws IOException {
        ResponseData responseUtils=new ResponseData();
        responseUtils.setCode(response.getStatusLine().getStatusCode());
        String data=EntityUtils.toString(response.getEntity());
        log.info("data={}",data);
        if(data!=null && data.startsWith("{")){
            responseUtils.setBody(JSONObject.parseObject(data));
        }
        return responseUtils;
    }
    //发送get请求
    public static ResponseData get(String url, Map<String,String> query_path) throws URISyntaxException, IOException {
        log.info("url={} query_path={}",url,query_path);
        String new_url=build_url(url,query_path);
        HttpGet httpGet=new HttpGet(new_url);
        CloseableHttpResponse response= httpClient.execute(httpGet);
        ResponseData responseUtils=build_response(response);
        response.close();
        return responseUtils;
    }
    //方法重载 ---get无参请求
    public static ResponseData get(String url) throws IOException, URISyntaxException {
        return get(url,null);
    }
    //发送post--form请求
    public static ResponseData post_form(String url, Map<String,String> query_path, Map<String,String> form_data) throws URISyntaxException, IOException {
        log.info("url={} query_path={} form_data={}",url,query_path,form_data);
        String new_url=build_url(url,query_path);
        HttpPost httpPost=new HttpPost(new_url);
        if(form_data!=null){
            List<NameValuePair> list=new ArrayList<>();
            for(String key: form_data.keySet()){
                list.add(new BasicNameValuePair(key, form_data.get(key)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(list,"utf-8"));
        }
        CloseableHttpResponse response= httpClient.execute(httpPost);
        ResponseData responseData=build_response(response);
        response.close();
        return responseData;

    }
    //发送post请求--无路径参数
    public static ResponseData post_form(String url,Map<String,String> form_data) throws IOException, URISyntaxException {
        return post_form(url,null,form_data);
    }
}
